using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services.Interfaces;
using System.Threading.Tasks;
using System;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ProductionStockInsController : ControllerBase
    {
        private readonly IProductionStockInService _productionStockInService;

        public ProductionStockInsController(IProductionStockInService productionStockInService)
        {
            _productionStockInService = productionStockInService;
        }

        // GET: api/ProductionStockIns
        [HttpGet]
        public async Task<ActionResult<object>> GetProductionStockIns(int page = 1, int pageSize = 10)
        {
            var total = await _productionStockInService.GetTotalProductionStockInsAsync();
            var stockIns = await _productionStockInService.GetProductionStockInsPagedAsync(page, pageSize);
            return Ok(new { items = stockIns, total });
        }

        // GET: api/ProductionStockIns/5
        [HttpGet("{id}")]
        public async Task<ActionResult<ProductionStockIn>> GetProductionStockIn(int id)
        {
            var stockIn = await _productionStockInService.GetProductionStockInByIdAsync(id);

            if (stockIn == null)
            {
                return NotFound();
            }

            return stockIn;
        }

        // GET: api/ProductionStockIns/code/{stockInCode}
        [HttpGet("code/{stockInCode}")]
        public async Task<ActionResult<ProductionStockIn>> GetProductionStockInByCode(string stockInCode)
        {
            var stockIn = await _productionStockInService.GetProductionStockInByCodeAsync(stockInCode);

            if (stockIn == null)
            {
                return NotFound();
            }

            return stockIn;
        }

        // PUT: api/ProductionStockIns/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutProductionStockIn(int id, ProductionStockIn stockIn)
        {
            if (id != stockIn.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _productionStockInService.UpdateProductionStockInAsync(stockIn);
                if (!updated)
                {
                    return NotFound();
                }
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }

            return NoContent();
        }

        // POST: api/ProductionStockIns
        [HttpPost]
        public async Task<ActionResult<ProductionStockIn>> PostProductionStockIn(ProductionStockIn stockIn)
        {
            try
            {
                var createdStockIn = await _productionStockInService.CreateProductionStockInAsync(stockIn);
                return CreatedAtAction("GetProductionStockIn", new { id = createdStockIn.Id }, createdStockIn);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/ProductionStockIns/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteProductionStockIn(int id)
        {
            var deleted = await _productionStockInService.DeleteProductionStockInAsync(id);
            if (!deleted)
            {
                return NotFound();
            }

            return NoContent();
        }

        // PATCH: api/ProductionStockIns/5/status
        [HttpPatch("{id}/status")]
        public async Task<IActionResult> ChangeProductionStockInStatus(int id, [FromBody] ProductionStatusChangeRequest request)
        {
            var success = await _productionStockInService.ChangeProductionStockInStatusAsync(id, request.Status, request.UpdatedBy);
            if (!success)
            {
                return NotFound();
            }

            return NoContent();
        }

        public class ProductionStatusChangeRequest
        {
            public string Status { get; set; }
            public int UpdatedBy { get; set; }
        }

        // PATCH: api/ProductionStockIns/5/approve
        [HttpPatch("{id}/approve")]
        public async Task<IActionResult> ApproveProductionStockIn(int id, [FromBody] ProductionApproveRequest request)
        {
            try
            {
                if (!int.TryParse(request.ApprovedBy, out int approverId))
                {
                    return BadRequest("审批人ID必须是有效的整数");
                }
                var success = await _productionStockInService.ApproveProductionStockInAsync(id, approverId, request.ApprovalRemark);
                if (!success)
                {
                    return NotFound();
                }
                return NoContent();
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (InvalidOperationException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        public class ProductionApproveRequest
        {
            public string ApprovedBy { get; set; }
            public string ApprovalRemark { get; set; }
        }
    }
}